<?php

require_once('rewrite_rules.php');

$url = '/';
if (isset($_GET['url'])) {
    $url = $_GET['url'];
}
$vars = array();
foreach ($rules as $rule => $rule_vars) {       
    if (preg_match('#^'. $rule .'$#', $url, $matches)) {        
        foreach ($rule_vars as $var => $index) {
            if (is_int($index)) {
                if (isset($matches[$index])) {
                    $vars[$var] = $matches[$index];
                } else {
                    unset($vars[$var]);
                }
            } else {
                $vars[$var] = $index;
            }
        }
        break;
    }
}

// NEW
if (isset($_GET['module'])) {    
    setCurrentModule($_GET['module']);
}
if (isset($_POST['module'])) {
    setCurrentModule($_POST['module']);
}
if (isset($_GET['request'])) {
    setCurrentRequest($_GET['request']);
}
if (isset($_POST['request'])) {
    setCurrentRequest($_POST['request']);
}


foreach ($vars as $var => $value) {    
    //global ${$var};
    //${$var} = $value;
    //$_GET[$var] = $value;
    //$_REQUEST[$var] = $value;
    if ($var == 'module') {        
        setCurrentModule($value);
    } elseif ($var == 'request') {
        setCurrentRequest($value);
    } else {
        setCurrentRequestParameters($var, $value);
    }
}

if (!getCurrentModule()) {    
    if(trim($_SERVER['QUERY_STRING']) == ""){
            setCurrentModule('index');
    } else {       
        show404ErrorPage();
    }
    
    
}
